package com.example.gokchinesefoodmappcdev.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.gokchinesefoodmappcdev.dao.*;
import com.example.gokchinesefoodmappcdev.entity.*;
import com.example.gokchinesefoodmappcdev.entity.dto.GoodsDTO;
import com.example.gokchinesefoodmappcdev.entity.dto.GoodsInfoAndGoodsSkuDTO;
import com.example.gokchinesefoodmappcdev.entity.dto.GoodsInfoExcelDTO;
import com.example.gokchinesefoodmappcdev.entity.dto.UpdateGoodsDTO;
import com.example.gokchinesefoodmappcdev.service.GoodsInfoService;
import com.example.gokchinesefoodmappcdev.util.ResultData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsInfoServiceImpl implements GoodsInfoService {

    @Autowired
    GoodsInfoDao goodsInfoDao;
    @Autowired
    private GoodsPicDao goodsPicDao;
    @Autowired
    private GoodsAttrKeyDao goodsAttrKeyDao;
    @Autowired
    private GoodsAttrValDao goodsAttrValDao;
    @Autowired
    private GoodsSkuDao goodsSkuDao;


    /*
    * 查询商品信息，存入GoodsInfoExcelDTO，导出excel表格
    * */
    @Override
    public List<GoodsInfoExcelDTO> getGoodsInfo() {
        List<GoodsInfoExcelDTO> goodsInfo = goodsInfoDao.getGoodsInfo();
        return goodsInfo;
    }

    /*
    * 修改商品信息
    * */
    @Override
    public ResultData updateGoodsInfo(UpdateGoodsDTO goodsDTO) {

        int i = goodsInfoDao.updateGoodsInfo(goodsDTO);
        if(i>0){
            return new ResultData(0,"操作成功！");
        }

        return new ResultData(500,"操作失败！");
    }

    /*
    * 修改商品上架状态
    * */
    @Override
    public ResultData updateGoodsOnSale(int id, String onSale) {

        int i = goodsInfoDao.updateGoodsOnSale(id, onSale);
        if(i>0){
            return new ResultData(0,"操作成功！");
        }

        return new ResultData(500,"操作失败！");

    }

    /*
     * 根据传入的商品ID删除商品
     * */
    @Override
    public ResultData removeGoodById(Integer goodId) {

        int i = goodsInfoDao.deleteById(goodId);
        if(i>0){
            return new ResultData(0,"操作成功！");
        }

        return new ResultData(500,"操作失败！");

    }

    /*
    * 根据ID查询商品信息
    * */
    @Override
    public ResultData selectGoodsInfoById(int id) {

        GoodsInfo goodsInfo = goodsInfoDao.selectById(id);

        ResultData resultData=new ResultData();
        resultData.put("code",0);
        resultData.put("rows",goodsInfo);

        return resultData;


    }

    /*
    * 查询商品信息列表
    * */
    @Override
    public ResultData selectGoodsList(int pageSize, int pageNumber) {


        //开启分页，该方法调用需要在调用数据库之前
        PageHelper.startPage(pageNumber,pageSize);

        List<GoodsInfo> goodsInfos = goodsInfoDao.selectList(null);

        PageInfo<GoodsInfo> pageInfo=new PageInfo<>(goodsInfos);

        ResultData resultData=new ResultData();
        resultData.put("code",0);


        //构建翻页对象

        Map<String, Object> rows = new HashMap<>();

        rows.put("pageNo",pageInfo.getPageNum());
        rows.put("pageSize",pageInfo.getPageSize());

        //将翻页对象存入结果集

        rows.put("rows",goodsInfos);

        resultData.put("rows",rows);

        return resultData;
    }

    /*
    * 根据根据商品编码（唯一）获取商品ID
    * */
    @Override
    public Integer selectGoodsIdByGoodCode(GoodsDTO goodsInfo) {

        int goodId = goodsInfoDao.selectGoodsIdByGoodCode(goodsInfo);
        return goodId;
    }


    /*
    * 插入商品
    * */
    @Override
    public ResultData insertGoodsInfo(GoodsDTO goodsInfo) {

        int i = goodsInfoDao.insertGoodsInfo(goodsInfo);
        if(i>0){
            return new ResultData(0,"操作成功!");
        }

        return new ResultData(500,"操作失败");


    }

    /**
     * 分页查询
     * 根据条件查询所有商品
     * @param goodsInfoAndGoodsSkuDTO
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageBean<GoodsInfoAndGoodsSkuDTO> selectGoodsAll(GoodsInfoAndGoodsSkuDTO goodsInfoAndGoodsSkuDTO, int pageNum, int pageSize) {
        //使用PageHelper开启分页，该该方法调用需要在调用数据库的操作之前执行
        PageHelper.startPage(pageNum,pageSize);
        //查出数据结果集
        List<GoodsInfoAndGoodsSkuDTO> goodsList = goodsInfoDao.findAllGoods(goodsInfoAndGoodsSkuDTO);
        //获取总数据数
        int goodsNum = goodsInfoDao.countGoodsNum(goodsInfoAndGoodsSkuDTO);
        //将信息封装到PageBean中，返回出去
        PageBean<GoodsInfoAndGoodsSkuDTO> pageBean = new PageBean<>(pageNum,pageSize,goodsNum);
        pageBean.setRows(goodsList);
        return pageBean;
    }

    /**
     * 根据id查询商品详情
     * @param goodsInfoAndGoodsSkuDTO
     * @return
     */
    @Override
    public ResultData selectGoodsAndAttrKeyAndAttrValAndPicAndSku(GoodsInfoAndGoodsSkuDTO goodsInfoAndGoodsSkuDTO) {
        // 查询商品的信息
        List<GoodsInfoAndGoodsSkuDTO> allGoods = goodsInfoDao.findAllGoods(goodsInfoAndGoodsSkuDTO);
        GoodsInfoAndGoodsSkuDTO goodsInfoAndGoodsSku = allGoods.get(0);

        // 查询商品图片
        QueryWrapper<GoodsPic> goodsPicWrapper = new QueryWrapper();
        goodsPicWrapper.eq("good_id", goodsInfoAndGoodsSkuDTO.getId());
        List<GoodsPic> goodsPics = goodsPicDao.selectList(goodsPicWrapper);

        // 查询商品规格
        QueryWrapper<GoodAttrKey> goodAttrKeyWrapper = new QueryWrapper();
        goodAttrKeyWrapper.eq("good_id",goodsInfoAndGoodsSkuDTO.getId());
        List<GoodAttrKey> goodAttrKeys = goodsAttrKeyDao.selectList(goodAttrKeyWrapper);

        // 存放商品规格及其对应的规格值
        Map<String,Object> goodAttrKeyAndgoodAttrVal = new HashMap();
        for (GoodAttrKey goodAttrKey : goodAttrKeys) {
            // 查询商品规格值
            QueryWrapper<GoodAttrVal> goodAttrValWrapper = new QueryWrapper();
            goodAttrValWrapper.eq("attr_key_id",goodAttrKey.getId());
            List<GoodAttrVal> goodAttrVals = goodsAttrValDao.selectList(goodAttrValWrapper);
            goodAttrKeyAndgoodAttrVal.put("goodId",goodAttrKey.getGoodId());
            goodAttrKeyAndgoodAttrVal.put("attrName",goodAttrKey.getAttrName());
            goodAttrKeyAndgoodAttrVal.put("attrValues",goodAttrVals);
            goodAttrKeyAndgoodAttrVal.put("id",goodAttrKey.getId());
        }
        // 查询商品sku信息
        QueryWrapper<GoodsSku> goodsSkuWrapper = new QueryWrapper<>();
        goodsSkuWrapper.eq("good_id",goodsInfoAndGoodsSkuDTO.getId());
        List<GoodsSku> goodsSkus = goodsSkuDao.selectList(goodsSkuWrapper);

        Map<String, Object> specifications = new HashMap();
        specifications.put("speKeyValue",goodAttrKeyAndgoodAttrVal);
        specifications.put("goodSKUs",goodsSkus);

        goodsInfoAndGoodsSku.setSpecifications(specifications);
        goodsInfoAndGoodsSku.setPictures(goodsPics);

        return ResultData.createData(ResultData.OPERATION_SUCCESS,goodsInfoAndGoodsSku);
    }

    /**
     * 商品审核
     * @param id 商品id
     * @param reviewRet 审核状态
     * @return
     */
    @Override
    public ResultData updateGoodsInfoReviewStatusById(int id, String reviewRet) {
        QueryWrapper<GoodsInfo> goodsInfoWrapper = new QueryWrapper();
        goodsInfoWrapper.eq("id",id);
        GoodsInfo goodsInfo = new GoodsInfo();
        goodsInfo.setReviewStatus(reviewRet);
        goodsInfo.setId(id);
        int i = goodsInfoDao.update(goodsInfo, goodsInfoWrapper);
        if (i >0) {
            return ResultData.ok(ResultData.OPERATION_SUCCESS,"审核通过");
        }
        return ResultData.ok(ResultData.OPERATION_FAIL,"审核拒绝");
    }
}
